package com.gao.modules.poi;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author DarkRepulser
 * @date 2021/11/1 14:23
 * @desc PoI测试
 */
public class PoiController {

    @GetMapping(value = "/testPoiExcelCreate")
    public String testPoiExcelCreate() throws IOException {
        //1、创建一个excel对象
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //2、创建一个sheet
        HSSFSheet sheet = hssfWorkbook.createSheet("测试sheet");
        //3、开始创建行
        HSSFRow row = sheet.createRow(0);
        //4、创建列
        HSSFCell rowCell = row.createCell(0);
        rowCell.setCellValue("这是第一个小格");
        HSSFCell rowCell1 = row.createCell(1);
        rowCell1.setCellValue("测试1");
        HSSFRow row1 = sheet.createRow(1);
        HSSFCell cell = row1.createCell(0);
        cell.setCellValue("这是第二行的数据");


        //HSSFWorkbook提供了write的方法可以直接根据输出流就行写入

        FileOutputStream outputStream = new FileOutputStream("d:\\测试demo的表格excel.xls");
        hssfWorkbook.write(outputStream);
        hssfWorkbook.close();
        outputStream.close();
        return "成功啦！";
    }
}
